package com.itheima.question.controller;

import com.itheima.pojo.Config.Result;
import com.itheima.pojo.dto.PageDto;
import com.itheima.pojo.dto.QuestionDto;
import com.itheima.pojo.vo.PageVo;
import com.itheima.pojo.vo.QuestionVo;
import com.itheima.question.serivce.IQuestionSerivce;
import com.itheima.swagger.Config.FastDFSClientUtil;
import com.itheima.swagger.contants.RedisConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/question")
@Api(value = "问题模块接口", tags = "question_question", description = "用于实现后台问题的增删改查操作")
public class QuestionController {
    @Autowired
    private IQuestionSerivce questionSerivce;
    @Autowired
    FastDFSClientUtil fastDFSClientUtil;
    @Value("${fileServerUrl}")
    private String fileServerUrl;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/add")
    @ApiOperation("新增题目")
    public Result addQuestion(@RequestBody QuestionDto questionDto, HttpSession session) throws Exception {
        Boolean flag = questionSerivce.addQuestion(questionDto, session);
        questionDto.setId(session.getAttribute("question_id").toString());
        Result result = Result.builder()
                .data(questionDto)
                .flag(flag)
                .message(flag?"添加题目成功!" : "添加题目失败!")
                .build();
        return result;
    }

    @PostMapping("/list")
    @ApiOperation("分页显示题目列表")
    public PageVo<QuestionVo> QueryByPage(@RequestBody PageDto pageDto) {
        if (pageDto.getCurrentPage() == null) {
            pageDto.setCurrentPage(1);
        }
        if (pageDto.getPageSize() == null) {
            pageDto.setPageSize(10);
        }
        return questionSerivce.queryByPage(pageDto);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据题目id查找题目信息")
    public QuestionVo QueryById(@PathVariable("id") String id) {
        return questionSerivce.queryById(id);
    }
}
